package com.jp.knowledge.webUtil;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jp.knowledge.dialog.CommDialog;
import com.jp.knowledge.util.L;
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient.CustomViewCallback;
import com.tencent.smtt.export.external.interfaces.JsPromptResult;
import com.tencent.smtt.export.external.interfaces.JsResult;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.export.external.interfaces.WebResourceResponse;
import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebSettings.LayoutAlgorithm;
import com.tencent.smtt.sdk.WebStorage;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.util.HashMap;
import java.util.Map;

public class X5WebView extends WebView {
    public static final int FILE_CHOOSER = 0;
    private String resourceUrl = "";
    private WebView smallWebView;
    private static boolean isSmallWebViewDisplayed = false;
    private boolean isClampedY = false;
    private Map<String, Object> mJsBridges;
    private TextView tog;
    RelativeLayout.LayoutParams layoutParams;
    private RelativeLayout refreshRela;
    TextView title;

    private UrlHandler urlHandler;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
                loadUrl(msg.obj.toString());
        }
    };

    public void setUrlHandler(UrlHandler urlHandler) {
        this.urlHandler = urlHandler;
    }

    private WebViewClient client = new WebViewClient() {
        /**
         * 防止加载网页时调起系统浏览器
         */
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
            return true;
        }

        public void onReceivedHttpAuthRequest(WebView webview,
                                              com.tencent.smtt.export.external.interfaces.HttpAuthHandler httpAuthHandlerhost, String host,
                                              String realm) {
            boolean flag = httpAuthHandlerhost.useHttpAuthUsernamePassword();
            L.e("onReceivedHttpAuthRequest","========"+flag);
        }

        public void onPageStarted(final WebView webView, final String url, android.graphics.Bitmap favicon) {
//			if(urlHandler!=null)
//				urlHandler.handUrl(url);
//            handler.postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    if(webView.getProgress()<100){
//                        Message msg = handler.obtainMessage();
//                        msg.obj=url;
//                        handler.sendMessage(msg);
//                    }
//                }
//            },10*1000);
        }

        ;

        public void onPageFinished(WebView webView, String url) {
//            webView.loadUrl("file:///android_asset/error.html");
            if (urlHandler != null)
                urlHandler.handUrl(url);
        }

        @Override
        public void onReceivedError(WebView webView, int i, String s, String s1) {
            L.e("onReceivedError","网页加载错误......." + "i=" + i + "=====s=" + s + "===s1=" + s1);
            webView.stopLoading();
            webView.clearView();
//            webView.loadUrl("file:///android_asset/error.html");
            if(urlHandler!=null)
                urlHandler.handError(webView,i,s,s1);
        }

        @Override
        public void onReceivedHttpError(WebView webView, WebResourceRequest webResourceRequest, WebResourceResponse webResourceResponse) {
            L.e("onReceivedHttpError","网页加载错误......." + "i=================" );
            super.onReceivedHttpError(webView, webResourceRequest, webResourceResponse);
//            webView.loadUrl("file:///android_asset/error.html");
            if(urlHandler!=null)
                urlHandler.handError(webView,0,"","");
        }
    };


    private WebChromeClient chromeClient = new WebChromeClient() {

        @Override
        public boolean onJsConfirm(WebView arg0, String arg1, String arg2, JsResult arg3) {
            return super.onJsConfirm(arg0, arg1, arg2, arg3);
        }

        View myVideoView;
        View myNormalView;
        CustomViewCallback callback;

        ///////////////////////////////////////////////////////////
        //

        /**
         * 全屏播放配置
         */
        @Override
        public void onShowCustomView(View view, CustomViewCallback customViewCallback) {
            // FrameLayout normalView = (FrameLayout) ((Activity)
            // getContext()).findViewById(R.id.web_filechooser);
            // ViewGroup viewGroup = (ViewGroup) normalView.getParent();
            // viewGroup.removeView(normalView);
            // viewGroup.addView(view);
            myVideoView = view;
            // myNormalView = normalView;
            callback = customViewCallback;
        }

        @Override
        public void onHideCustomView() {
            if (callback != null) {
                callback.onCustomViewHidden();
                callback = null;
            }
            if (myVideoView != null) {
                ViewGroup viewGroup = (ViewGroup) myVideoView.getParent();
                viewGroup.removeView(myVideoView);
                viewGroup.addView(myNormalView);
            }
        }

        @Override
        public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType, String captureType) {
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("*/*");
            ((Activity) (X5WebView.this.getContext())).startActivityForResult(Intent.createChooser(i, "choose files"),
                    X5WebView.FILE_CHOOSER);
            super.openFileChooser(uploadFile, acceptType, captureType);
        }

        /**
         * webview 的窗口转移
         */
        @Override
        public boolean onCreateWindow(WebView arg0, boolean arg1, boolean arg2, Message msg) {
            if (X5WebView.isSmallWebViewDisplayed == true) {

                WebView.WebViewTransport webViewTransport = (WebView.WebViewTransport) msg.obj;
                WebView webView = new WebView(X5WebView.this.getContext()) {

                    protected void onDraw(Canvas canvas) {
                        super.onDraw(canvas);
                        Paint paint = new Paint();
                        paint.setColor(Color.GREEN);
                        paint.setTextSize(15);
                        canvas.drawText("新建窗口", 10, 10, paint);
                    }

                    ;
                };
                webView.setWebViewClient(new WebViewClient() {
                    public boolean shouldOverrideUrlLoading(WebView arg0, String arg1) {
                        arg0.loadUrl(arg1);
                        return true;
                    }

                    ;
                });
                FrameLayout.LayoutParams lp = new LayoutParams(400, 600);
                lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
                X5WebView.this.addView(webView, lp);
                webViewTransport.setWebView(webView);
                msg.sendToTarget();
            }
            return true;
        }
        private   CommDialog dialog  =null;
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            if(dialog==null){
                dialog= new CommDialog(getContext());
                dialog.setRightListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
            }
            dialog.setContent(message);
            dialog.show();
            result.confirm();
            return true;
//            return  super.onJsAlert(arg0, arg1, arg2, arg3);
        }

        /**
         * 对应js 的通知弹框 ，可以用来实现js 和 android之间的通信
         */
        @Override
        public boolean onJsPrompt(WebView arg0, String arg1, String arg2, String arg3, JsPromptResult arg4) {
            // 在这里可以判定js传过来的数据，用于调起android native 方法
            if (X5WebView.this.isMsgPrompt(arg1)) {
                if (X5WebView.this.onJsPrompt(arg2, arg3)) {
                    return true;
                } else {
                    return false;
                }
            }
            return super.onJsPrompt(arg0, arg1, arg2, arg3, arg4);
        }

        @Override
        public void onReceivedTitle(WebView arg0, final String arg1) {
            super.onReceivedTitle(arg0, arg1);
            Log.i("yuanhaizhou", "webpage title is " + arg1);

        }
    };

    /**
     * 屏蔽长按复制工
     *
     * @param onLongClickListener
     */
    @Override
    public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
//		super.setOnLongClickListener(onLongClickListener);
    }

    @SuppressLint("SetJavaScriptEnabled")
    public X5WebView(Context arg0, AttributeSet arg1) {
        super(arg0, arg1);
        this.setWebViewClientExtension(new X5WebViewEventHandler(this));// 配置X5webview的事件处理
        this.setWebViewClient(client);
        this.setWebChromeClient(chromeClient);
        WebStorage webStorage = WebStorage.getInstance();
        initWebViewSettings();
        this.getView().setClickable(true);
        this.getView().setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
    }

    private void initWebViewSettings() {
        WebSettings webSetting = this.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(true);
        webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true);
        webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);

        // this.getSettingsExtension().setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);//extension
        // settings 的设计
    }

    @SuppressLint("NewApi")
    @Override
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        boolean ret = super.drawChild(canvas, child, drawingTime);
        canvas.save();
        Paint paint = new Paint();
        paint.setColor(0x7fff0000);
        paint.setTextSize(24.f);
        paint.setAntiAlias(true);
        // if (getX5WebViewExtension() != null) {
        // canvas.drawText(this.getContext().getPackageName() + "-pid:" +
        // android.os.Process.myPid(), 10, 50, paint);
        // canvas.drawText("X5 Core:" + QbSdk.getTbsVersion(this.getContext()),
        // 10, 100, paint);
        // } else {
        // canvas.drawText(this.getContext().getPackageName() + "-pid:" +
        // android.os.Process.myPid(), 10, 50, paint);
        // canvas.drawText("Sys Core", 10, 100, paint);
        // }
        // canvas.drawText(Build.MANUFACTURER, 10, 150, paint);
        // canvas.drawText(Build.MODEL, 10, 200, paint);
        canvas.restore();
        return ret;
    }

    public X5WebView(Context arg0) {
        super(arg0);
        setBackgroundColor(85621);
    }

    public static void setSmallWebViewEnabled(boolean enabled) {
        isSmallWebViewDisplayed = enabled;
    }

    public void addJavascriptBridge(SecurityJsBridgeBundle jsBridgeBundle) {
        if (this.mJsBridges == null) {
            this.mJsBridges = new HashMap<String, Object>(5);
        }

        if (jsBridgeBundle != null) {
            String tag = SecurityJsBridgeBundle.BLOCK + jsBridgeBundle.getJsBlockName() + "-"
                    + SecurityJsBridgeBundle.METHOD + jsBridgeBundle.getMethodName();
            this.mJsBridges.put(tag, jsBridgeBundle);
        }
    }

    /**
     * 当webchromeClient收到 web的prompt请求后进行拦截判断，用于调起本地android方法
     *
     * @param methodName 方法名称
     * @param blockName  区块名称
     * @return true ：调用成功 ； false ：调用失败
     */
    private boolean onJsPrompt(String methodName, String blockName) {
        String tag = SecurityJsBridgeBundle.BLOCK + blockName + "-" + SecurityJsBridgeBundle.METHOD + methodName;

        if (this.mJsBridges != null && this.mJsBridges.containsKey(tag)) {
            ((SecurityJsBridgeBundle) this.mJsBridges.get(tag)).onCallMethod();
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判定当前的prompt消息是否为用于调用native方法的消息
     *
     * @param msg 消息名称
     * @return true 属于prompt消息方法的调用
     */
    private boolean isMsgPrompt(String msg) {
        if (msg != null && msg.startsWith(SecurityJsBridgeBundle.PROMPT_START_OFFSET)) {
            return true;
        } else {
            return false;
        }
    }

    private int startX;
    private int startY;
    boolean intercepted = false;
    // TBS: Do not use @Override to avoid false calls


    public boolean tbs_dispatchTouchEvent(MotionEvent ev, View view) {
        boolean r = super.super_dispatchTouchEvent(ev);
        Log.d("Bran", "dispatchTouchEvent " + ev.getAction() + " " + r);
        return r;

    }

    // TBS: Do not use @Override to avoid false calls
    public boolean tbs_onInterceptTouchEvent(MotionEvent ev, View view) {
        boolean r = super.super_onInterceptTouchEvent(ev);
        return r;
    }

    protected void tbs_onScrollChanged(int l, int t, int oldl, int oldt, View view) {
        super_onScrollChanged(l, t, oldl, oldt);
    }

    protected void tbs_onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY, View view) {
        super_onOverScrolled(scrollX, scrollY, clampedX, clampedY);
    }

    protected void tbs_computeScroll(View view) {
        super_computeScroll();
    }

    @SuppressLint("NewApi")
    protected boolean tbs_overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX,
                                       int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent, View view) {
        if (this.isClampedY) {
            if ((refreshRela.getTop() + (-deltaY)) / 2 < 255) {
                this.tog.setAlpha((refreshRela.getTop() + (-deltaY)) / 2);
            } else
                this.tog.setAlpha(255);
            this.refreshRela.layout(refreshRela.getLeft(), refreshRela.getTop() + (-deltaY), refreshRela.getRight(),
                    refreshRela.getBottom() + (-deltaY));
            this.layout(this.getLeft(), this.getTop() + (-deltaY) / 2, this.getRight(),
                    this.getBottom() + (-deltaY) / 2);
        }
        return super_overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX,
                maxOverScrollY, isTouchEvent);
    }

//    @Override
//    public void scrollBy(int x, int y) {
//        tbs_overScrollBy (x, y, x, y, x, y, x,
//                y, true,null);
//    }

    public void setTitle(TextView title) {
        this.title = title;
    }

    protected boolean tbs_onTouchEvent(MotionEvent event, View view) {
        return super_onTouchEvent(event);
    }

    public interface UrlHandler {
        void handUrl(String url);

        void handError(WebView webView, int i, String s, String s1);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        L.e("onTouchEvent===x5web======x=" + event.getX() + "========y=" + event.getY());
        return super.onTouchEvent(event);
    }

}
